package com.cn.Dao;

import com.cn.base.BaseDao;
import com.cn.entity.AdminInfo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {
    public AdminInfo findByName(AdminInfo adminInfo) throws SQLException {
        Connection connection = BaseDao.getConn();
        try {
            Statement state=connection.createStatement();
            String sql="select * from admin where user = '" +
                    adminInfo.getUsername() + "'";
            ResultSet rs = state.executeQuery(sql);
            adminInfo = getAdmin(rs);
            connection.close();
            return adminInfo;
        }catch (Exception e){
            throw e;
        }
    }
    private AdminInfo getAdmin(ResultSet resultSet){
        try {
            AdminInfo adminInfo = new AdminInfo();
            while (resultSet.next()){
                adminInfo.setUsername(resultSet.getString(1));
                adminInfo.setPassword(resultSet.getString(2));
            }
            return adminInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    private List<AdminInfo> getList(ResultSet resultSet){
        try {
            List<AdminInfo> adminInfos = new ArrayList<>();
            while (resultSet.next()){

                AdminInfo adminInfo = new AdminInfo();
                adminInfo.setUsername(resultSet.getString(1));
                adminInfo.setPassword(resultSet.getString(2));
                adminInfos.add(adminInfo);
            }
            return adminInfos;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
